package com.ctgu.tengyun.filter;

import com.ctgu.tengyun.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class H5XCrossAllowFilter implements Filter {

	private static final Logger logger = LoggerFactory.getLogger(H5XCrossAllowFilter.class);
	
	public void init(FilterConfig config) throws ServletException {
		logger.info("init H5AuthorizationFilter");
	}

	public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) {
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpServletRequest requestWrpper = (HttpServletRequest)request;
		
		httpResponse.setHeader("Access-Control-Allow-Origin", "*");	
		httpResponse.setHeader("Access-Control-Allow-Headers","Content-Type,si,tk,sg");
		
		try {
			chain.doFilter(requestWrpper, response);
		} catch (IOException e) {
			httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			logger.info("auth filter error. {}",  ServletUtil.getRemoteAddressV2(requestWrpper), e);
		} catch (ServletException e) {
			httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			logger.info("auth filter error. {}", ServletUtil.getRemoteAddressV2(requestWrpper), e);
		} catch (Exception e) {
			httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			logger.info("auth filter error. {}", ServletUtil.getRemoteAddressV2(requestWrpper), e);
		}
	}

	public void destroy() {}

}